package com.ygqh.baby.handler;

import com.ygqh.baby.model.SkuExcelModel;
import com.ygqh.baby.po.AdminSku;
import com.ygqh.baby.service.AdminSkuService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: sunshuo
 * @date: 2018/12/11 16:02
 */
@Component
public class SkuExcelHandler {

	@Autowired
	private AdminSkuService adminSkuService;

	public List<SkuExcelModel> handlerExcelInfo(List<Map<Integer, Object>> excelInfo, SkuExcelModel.SkuExcelType skuExcelType) {
		Assert.notEmpty(excelInfo, "excel数据为空。");
		List<SkuExcelModel> detailList = new ArrayList<>(excelInfo.size());
		List<String> skuCodeList = new ArrayList<>(excelInfo.size());
		excelInfo.forEach(map -> {
			SkuExcelModel detail = processSkuFromExcelRow(map, skuExcelType);
			if (detail != null) {
				detailList.add(detail);
				skuCodeList.add(detail.getSkuCode());
			}
		});
		validateDetail(skuCodeList, detailList, skuExcelType);
		return detailList;
	}

	private void validateDetail(List<String> skuCodeList, List<SkuExcelModel> detailList, SkuExcelModel.SkuExcelType skuExcelType) {
		Assert.notEmpty(detailList, "数据为空。");
		Assert.notEmpty(skuCodeList, "不存在有效数据。");
		// 查询存在的SKU库存信息
		List<AdminSku> detailListByExists = adminSkuService.findListBySkuCode(skuCodeList);
		if (detailListByExists == null) {
			detailListByExists = new ArrayList<>();
		}
		Map<String, List<AdminSku>> collect = detailListByExists.stream().collect(Collectors.groupingBy(AdminSku::getSkuCode));
		detailList.forEach(detail -> {
			String skuCode = detail.getSkuCode();
			if (StringUtils.isBlank(skuCode)) {
				detail.setErrMsg("SKU编码为空。");
			} else if (SkuExcelModel.SkuExcelType.SkuPrice.equals(skuExcelType) && detail.getPrice() == null) {
				detail.setErrMsg("SKU价格为空");
			} else if (SkuExcelModel.SkuExcelType.SkuQuantity.equals(skuExcelType) && detail.getQuantity() == null) {
				detail.setErrMsg("SKU数量为空");
			} else if (MapUtils.isNotEmpty(collect) && collect.containsKey(skuCode)) {
				BeanUtils.copyProperties(collect.get(skuCode).get(0), detail);
			} else {
				detail.setErrMsg("SKU编码不存在。");
			}
		});
	}

	private SkuExcelModel processSkuFromExcelRow(Map<Integer, Object> infoMap, SkuExcelModel.SkuExcelType skuExcelType) {
		SkuExcelModel detail = new SkuExcelModel();
		boolean isEmpty = true;
		for (Map.Entry<Integer, Object> entry : infoMap.entrySet()) {
			Object value = entry.getValue();
			if (value == null || StringUtils.isBlank(value.toString())) {
				continue;
			}
			isEmpty = false;
			processSkuFromExcelCell(entry.getKey(), entry.getValue(), detail, skuExcelType);
		}
		if (isEmpty) {
			return null;
		}
		return detail;
	}

	private void processSkuFromExcelCell(Integer key, Object val, SkuExcelModel detail, SkuExcelModel.SkuExcelType skuExcelType) {
		switch (key) {
			case 0:
				detail.setProductName(val.toString());
				break;
			case 1:
				detail.setColor(val.toString());
				break;
			case 2:
				detail.setSizeName(val.toString());
				break;
			case 3:
				detail.setSkuCode(val.toString());
				break;
			case 4:
				if (SkuExcelModel.SkuExcelType.SkuPrice.equals(skuExcelType)) {
					DecimalFormat df = new DecimalFormat("0.00");
					BigDecimal price = StringUtils.isBlank(val.toString()) ? null : new BigDecimal(df.format(val));
					detail.setPrice(price);
				} else if (SkuExcelModel.SkuExcelType.SkuQuantity.equals(skuExcelType)) {
					DecimalFormat df = new DecimalFormat("0");
					Long quantity = val == null ? null : Long.valueOf(df.format(val));
					detail.setQuantity(quantity);
				}
				break;
			default:
				break;
		}
	}
}
